package com.ruoyi.system.service.impl

import com.ruoyi.common.constant.UserConstants
import com.ruoyi.common.core.domain.entity.SysDictData
import com.ruoyi.common.core.domain.entity.SysDictType
import com.ruoyi.common.exception.ServiceException
import com.ruoyi.common.utils.DictUtils
import com.ruoyi.common.utils.DictUtils.getDictCache
import com.ruoyi.common.utils.DictUtils.removeDictCache
import com.ruoyi.common.utils.DictUtils.setDictCache
import com.ruoyi.common.utils.MyStringUtils
import com.ruoyi.system.mapper.SysDictDataMapper
import com.ruoyi.system.mapper.SysDictTypeMapper
import com.ruoyi.system.service.ISysDictTypeService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import javax.annotation.PostConstruct

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
@Component
open class SysDictTypeServiceImpl : ISysDictTypeService {

    @Autowired
    private lateinit var dictTypeMapper: SysDictTypeMapper

    @Autowired
    private lateinit var dictDataMapper: SysDictDataMapper

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    fun init() {
        loadingDictCache()
    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    override fun selectDictTypeList(dictType: SysDictType?): List<SysDictType> {
        return dictTypeMapper.selectDictTypeList(dictType)
    }

    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    override fun selectDictTypeAll(): List<SysDictType> {
        return dictTypeMapper.selectDictTypeAll()
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    override fun selectDictDataByType(dictType: String?): List<SysDictData?>? {
        var dictDatas = getDictCache(dictType!!)
        if (MyStringUtils.isNotEmpty(dictDatas!!)) {
            return dictDatas
        }
        dictDatas = dictDataMapper.selectDictDataByType(dictType)
        if (MyStringUtils.isNotEmpty(dictDatas)) {
            setDictCache(dictType, dictDatas)
            return dictDatas
        }
        return null
    }

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    override fun selectDictTypeById(dictId: Long?): SysDictType? {
        return dictTypeMapper.selectDictTypeById(dictId)
    }

    /**
     * 根据字典类型查询信息
     *
     * @param dictType 字典类型
     * @return 字典类型
     */
    override fun selectDictTypeByType(dictType: String?): SysDictType? {
        return dictTypeMapper.selectDictTypeByType(dictType)
    }

    /**
     * 批量删除字典类型信息
     *
     * @param dictIds 需要删除的字典ID
     */
    override fun deleteDictTypeByIds(dictIds: Array<Long>) {
        for (dictId in dictIds) {
            val dictType = selectDictTypeById(dictId)
            if (dictDataMapper.countDictDataByType(dictType!!.dictType) > 0) {
                throw ServiceException(String.format("%1\$s已分配,不能删除", dictType.dictName))
            }
            dictTypeMapper.deleteDictTypeById(dictId)
            removeDictCache(dictType.dictType)
        }
    }

    /**
     * 加载字典缓存数据
     */
    override fun loadingDictCache() {
        val dictData = SysDictData()
        dictData.status = "0"
        val dictDataMap = dictDataMapper.selectDictDataList(dictData).groupBy { it.dictType }
        for ((key, value) in dictDataMap) {
            setDictCache(key, value.sortedBy { it.dictSort }.toMutableList())
        }
    }

    /**
     * 清空字典缓存数据
     */
    override fun clearDictCache() {
        DictUtils.clearDictCache()
    }

    /**
     * 重置字典缓存数据
     */
    override fun resetDictCache() {
        clearDictCache()
        loadingDictCache()
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dict 字典类型信息
     * @return 结果
     */
    override fun insertDictType(dict: SysDictType): Int {
        val row = dictTypeMapper.insertDictType(dict)
        if (row > 0) {
            setDictCache(dict.dictType, null)
        }
        return row
    }

    /**
     * 修改保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Transactional
    override fun updateDictType(dictType: SysDictType): Int {
        val oldDict = dictTypeMapper.selectDictTypeById(dictType.dictId)
        dictDataMapper.updateDictDataType(oldDict!!.dictType, dictType.dictType)
        val row = dictTypeMapper.updateDictType(dictType)
        if (row > 0) {
            val dictDatas = dictDataMapper.selectDictDataByType(dictType.dictType)
            setDictCache(dictType.dictType, dictDatas)
        }
        return row
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    override fun checkDictTypeUnique(sysDictType: SysDictType): Boolean {
        val dictId = if (MyStringUtils.isNull(sysDictType.dictId)) -1L else sysDictType.dictId
        val dictType = dictTypeMapper.checkDictTypeUnique(sysDictType.dictType)
        return if (MyStringUtils.isNotNull(dictType) && dictType!!.dictId != dictId) {
            UserConstants.NOT_UNIQUE
        } else UserConstants.UNIQUE
    }
}
